#pragma once
#include "include.h"

class CEventSocket;

typedef void (*PFN_SOCK_ON_EVENT) ( CEventSocket * sock, unsigned char event, void * pParam );

enum sock_state_t
{ 
	SOCK_STATE_NOTSOCK = 1, 
	SOCK_STATE_CREATED, 
	SOCK_STATE_BINDED,
	SOCK_STATE_LISTENING, 
	SOCK_STATE_CONNECTING, 
	SOCK_STATE_CONNECTED, 
	SOCK_STATE_CONNECT_FAILED,
	SOCK_STATE_CLOSED
};

enum sock_event_t
{ 
	SOCK_EVENT_CAN_READ = 1,
	SOCK_EVENT_CAN_WRITE,
	SOCK_EVENT_CAN_READ_OOB,
	SOCK_EVENT_CAN_ACCEPT,
	SOCK_EVENT_CONNECTING,
	SOCK_EVENT_CONNECT_OK,
	SOCK_EVENT_CONNECT_FAILED,
	SOCK_EVENT_CLOSED,
};

class CEventSocket : public base_sock
{
public:
	CEventSocket(void);
public:
	~CEventSocket(void);
public:
	void SetCallback( PFN_SOCK_ON_EVENT arg );
	bool Create( int nProtocol = IPPROTO_TCP, unsigned short uPort = 0 );
	bool Listen( int nBlock = 5 );
	bool Connect( SOCKADDR_IN * pAddr );
	bool Bind( SOCKADDR_IN * pAddr );
	void ShutDown( int nArg );
	void Close();
	void SetParam( void * arg );
	unsigned char GetState();
	bool CanRecv();
protected:
	virtual void OnReceive( int nErrorCode );
	virtual void OnSend( int nErrorCode );
	virtual void OnConnect( int nErrorCode );
	virtual void OnClose( int nErrorCode );
	virtual void OnAccept( int nErrorCode );
	virtual void OnOutOfBandData( int nErrorCode );
protected:
	PFN_SOCK_ON_EVENT m_pfCallback;
	unsigned char m_uState;
	void * m_pParam;
	bool m_bCanRecv;
};
